using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using DM.Entity;

namespace DM.DAO
{
    public class LoaiTinDAO:BaseDao<LoaiTin>
    {
        public override LoaiTin Get(int id)
        {
            LoaiTin loaiTin = new LoaiTin();
            SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.DMConnnectString, "getLoaiTinByMa", id);
            try
            {
                if (reader.Read())
                {
                    loaiTin.Ma = Common.ParseInt32(reader["Ma"].ToString());
                    loaiTin.Maloaicha = Common.ParseInt32(reader["MaLoaiCha"].ToString());
                    loaiTin.Mota = reader["MoTa"].ToString();
                    loaiTin.Ngonngu = Common.ParseInt32(reader["NgonNgu"].ToString());
                    loaiTin.Tenloaitin = reader["TenLoaiTin"].ToString();
                }
            }
            catch (Exception)
            {
                Common.Show("Error Connection!");
                throw;
            }
            finally {reader.Close();}
            
            return loaiTin;
        }

        public override LoaiTin Get(string name)
        {
            throw new NotImplementedException();
        }

        public override int Insert(LoaiTin t)
        {
            throw new NotImplementedException();
        }

        public override int Update(LoaiTin t)
        {
            throw new NotImplementedException();
        }

        public override int Update(List<LoaiTin> ts)
        {
            throw new NotImplementedException();
        }

        public override int Delete(string id)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// Lấy ra một danh sách các loại tin có mã tin cha theo yêu cầu
        /// </summary>
        /// <param name="maloaicha">Mã loại tin cha</param>
        /// <returns>List các LoaiTin có cùng mã tin cha</returns>
        public List<LoaiTin> GetLoaiTin(int maloaicha)
        {
            List<LoaiTin> list = new List<LoaiTin>();
            SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.DMConnnectString, "getLoaiTinByMaTinCha", maloaicha);
            try
            {
                while (reader.Read())
                {
                    LoaiTin loaiTinEntity = new LoaiTin();
                    loaiTinEntity.Ma = Common.ParseInt32(reader["Ma"].ToString());
                    loaiTinEntity.Maloaicha = Common.ParseInt32(reader["MaLoaiCha"].ToString());
                    loaiTinEntity.Mota = reader["MoTa"].ToString();
                    loaiTinEntity.Ngonngu = Common.ParseInt32(reader["NgonNgu"].ToString());
                    loaiTinEntity.Tenloaitin = reader["TenLoaiTin"].ToString();
                    list.Add(loaiTinEntity);
                }
            }
            catch(Exception)
            {
                Common.Show("Get hinh error!");
            }
            finally
            {
                reader.Close();
            }

            return list;
        }
        /// <summary>
        /// Lấy ra các Entity LoaiTin gốc (Loại tin cha là Null)
        /// </summary>
        /// <returns>Danh sách các Entity Loại Tin</returns>
        public  List<LoaiTin> getLoaiTinCha()
        {
            List<LoaiTin> list = new List<LoaiTin>();
            SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.DMConnnectString, "getLoaiTinGoc");
            try
            {
                while (reader.Read())
                {
                    LoaiTin loaiTinEntity = new LoaiTin();
                    loaiTinEntity.Ma = Common.ParseInt32(reader["Ma"].ToString());
                    loaiTinEntity.Maloaicha = Common.ParseInt32(reader["MaLoaiCha"].ToString());
                    loaiTinEntity.Mota = reader["MoTa"].ToString();
                    loaiTinEntity.Ngonngu = Common.ParseInt32(reader["NgonNgu"].ToString());
                    loaiTinEntity.Tenloaitin = reader["TenLoaiTin"].ToString();
                    list.Add(loaiTinEntity);
                }
            }
            catch(Exception)
            {
                Common.Show("Error connection!");
            }
            finally {reader.Close();}
            
            return list;
        }

        public LoaiTin getLoaiTinCha(Int32 maLoaiTin)
        {
            LoaiTin loaiTinEntity = new LoaiTin();
            SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.DMConnnectString, "getLoaiTinChaByMaTinCon", maLoaiTin);
            try
            {
                if (reader.Read())
                {

                    loaiTinEntity.Ma = Common.ParseInt32(reader["Ma"].ToString());
                    loaiTinEntity.Maloaicha = Common.ParseInt32(reader["MaLoaiCha"].ToString());
                    loaiTinEntity.Mota = reader["MoTa"].ToString();
                    loaiTinEntity.Ngonngu = Common.ParseInt32(reader["NgonNgu"].ToString());
                    loaiTinEntity.Tenloaitin = reader["TenLoaiTin"].ToString();
                }
            }
            catch (Exception)
            {
                Common.Show("Error Connection!");
                throw;
            }
            finally {reader.Close();}
            
            return loaiTinEntity;
        }


        /// <summary>
        /// Lấy ra mã LoaiTin gốc mà tin đó thuộc
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LoaiTin GetLoaiTinChaByMaTin(string id)
        {
            LoaiTin loaiTin = new LoaiTin();
            SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.DMConnnectString, "getLoaiTinGocByMaTin",
                                                               Common.ParseInt32(id));
            try
            {
                if (reader.Read())
                {
                    loaiTin.Ma = Common.ParseInt32(reader["Ma"].ToString());
                    loaiTin.Maloaicha = Common.ParseInt32(reader["MaLoaiCha"].ToString());
                    loaiTin.Mota = reader["MoTa"].ToString();
                    loaiTin.Ngonngu = Common.ParseInt32(reader["NgonNgu"].ToString());
                    loaiTin.Tenloaitin = reader["TenLoaiTin"].ToString();
                }
            }
            catch (Exception)
            {
                Common.Show("Error connection!");
                throw;
            }
            finally {reader.Close();}
            
            return loaiTin;
        }
    }
}
